var xmlHttp; 

function createXMLRequest () {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
//注册表单验证
function check (method) {   
	createXMLRequest();   
	var pwd=document.getElementById("password").value;
	var email=document.getElementById("email").value;
	var position=document.getElementById("position").value;
	var age=document.getElementById("age").value;
	var sex=document.getElementById("sex").value;
	var work=document.getElementById("work").value;      
	if (method == 1) {
		var parameter = document.getElementById('name').value
		if (parameter.length<4) {
			document.getElementById('userName').style.color = "#FFFAFA";
			document.getElementById('userName').innerHTML = "用户名长度太短！";
			document.getElementById('wtf').disabled=true;
			return false;
		} else {
			document.getElementById('userName').innerHTML = "";
		}
	} else if (method == 2) {
		var parameter = document.getElementById('email').value
		way=1
	} else if (method == 3) {
		var parameter = document.getElementById('password').value
		if (parameter.length<4) {
			document.getElementById('userPwd').style.color = "#FFFAFA";
			document.getElementById('userPwd').innerHTML = "密码长度太短！";
			document.getElementById('wtf').disabled=true;
			return false;
		} else {
			document.getElementById('userPwd').innerHTML = "";
		}
	} else if (method == 4) {
		var parameter = document.getElementById('email').value
		way=2
	} else if (method == 5) {
		if (isNaN(document.getElementById('age').value)) {
            alert('请输入正确的年龄');
            //document.getElementById('ageresult').innerHTML="请输入正确的年龄";
            document.getElementById('wtf').disabled=true;
            document.getElementById('age').value="";
        } else {
        	document.getElementById('wtf').disabled=false;
        }
	}  else if (method == 6) {
		if (pwd=="" || position=="" || age=="" || sex=="" || work==""){
			document.getElementById('wtf').disabled=true;
		} else {
			document.getElementById('wtf').disabled=false;
		}
		return false;
	}

	var url="/register/check/?parameter="+parameter+"&method="+way;
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = function (e) {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (parseInt(xmlHttp.responseText)==2) {
				if (way == 1) {
					document.getElementById('userEmail').style.color = "#FFFAFA";
					document.getElementById('userEmail').innerHTML = "您的邮箱已经注册!";
					if (pwd=="" || position=="" || age=="" || sex=="" || work==""){
						document.getElementById('wtf').disabled=true;
					} else {
						document.getElementById('wtf').disabled=false;
					}
					return false;
				} else if (way == 2) {
					document.getElementById('userEmail').style.color = "#FFFAFA";
					document.getElementById('userEmail').innerHTML = "您的邮箱已经申请!";
					if (pwd=="" || position=="" || age=="" || sex=="" || work==""){
						document.getElementById('wtf').disabled=true;
					} else {
						document.getElementById('wtf').disabled=false;
					}
					return false;
				}
			} else {
				document.getElementById('userEmail').innerHTML = "";
				if (pwd=="" || position=="" || age=="" || sex=="" || work==""){
					document.getElementById('wtf').disabled=true;
				} else {
					document.getElementById('wtf').disabled=false;
				}
			}
		}
	};
	xmlHttp.send(); 
}
//找回密码
function findPwd () {
	createXMLRequest();  
	var email=document.getElementById('findEmail').value;
	var url="/register/find/?email="+email;
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = function (e) {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (parseInt(xmlHttp.responseText)==1) {
				document.getElementById('findResult').style.color = "#FFFAFA";
				document.getElementById('findResult').innerHTML = "已经向您的邮箱发送重置密码i链接，一小时内有效！";
			} else {
				document.getElementById('findResult').innerHTML = "";
			}
		}
	};
	xmlHttp.send(); 
}